#include <fstream>
#include <boost/filesystem/path.hpp>
#include <iostream>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <boost/lexical_cast.hpp>
#include "../components/CmdLine/cmdargs.h"
#include <boost/regex.hpp>
#include "get_arg.h"
using std::string;
using std::ofstream;
using std::ifstream;
using std::cerr;
using std::cout;
using std::max;
using boost::lexical_cast;





//_______________________________________________________________________________________

//		GetArguments

//				from command line
//				Created [9/13/2001]

//_______________________________________________________________________________________
bool GetArguments(int argc, char* argv[], t_program_args& args)
{
	// optional help directive
	CmdArgUsage opt_help('h', "help", 	"display this message", &cout);

	// required options
	const unsigned REQUIRED = CmdArg::isREQ | CmdArg::isVALREQ;
	CmdArgStr opt_dbname('d', "dbname", "<string>",
								"PostgreSQL database name. ", REQUIRED);
	CmdArgStr opt_host	('h', "host", "<string>",
								"PostgreSQL database host computer address. ");
	CmdArgStr opt_user	('u', "user", "<string>",
								"PostgreSQL database user name. Defaults to 'postgres'.");
	CmdArgStr opt_password('p', "password", "<string>",
								"PostgreSQL database password.");
	CmdArgStr opt_login	('l', "login", "<string>",
								"Other PostgreSQL login parameters in the form of "
								"keyword = value. E.g \"port=1234\"");
	
	CmdArgFloat opt_ks_cutoff	('c', "cutoff_for_ks", "<double>",
								"Pairs with greater Ks will not be included", REQUIRED);
	CmdArgFloat opt_filter_cutoff('f', "filter_cutoff", "<double>",
								"Pairs joining onto the filter table will not be "
								"included if they fall outside the filter (see the "
								"--less_than option).", REQUIRED);
	
	
	CmdArgStr opt_ks_table 		('k', "ks_table", "<string>",
								"table with ks and gene pairs ", REQUIRED);
	CmdArgStr opt_filter_column	('c', "column_filter", "<string>",
								"table with ks and gene pairs ", REQUIRED);
	CmdArgStr opt_filter_table	('t', "table_filter", "<string>",
								"table for obtaining values to filter ks pairs in a join ",
								REQUIRED);
	CmdArgSet opt_filter_less_than('l', "less_than",
									"filter on values less than the supplied cut off. "
									"Defaults to false");
	
	CmdArgSet opt_verbose('v', "verbose",
								"show progress and issue lots more messages.");
	CmdArgStr opt_err_log ('e', "err_log", "<string>",
								"file for logging bl2seq failures "
								"(defaults to STDERR).");

	string exe_file_name =  boost::filesystem::path(*argv).leaf();
	
		


		// construct command line
	CmdLine  cmdline(exe_file_name.c_str(),
							&opt_ks_cutoff			,
							&opt_filter_cutoff		,
							&opt_ks_table 			,
							&opt_filter_column		,
							&opt_filter_table		,
							&opt_filter_less_than	,
							&opt_dbname,
							&opt_host,
							&opt_user,
							&opt_password,
							&opt_login,
							&opt_err_log,
							&opt_help,
							&opt_verbose,
							0);

	// parse command line
	CmdArgvIter  argv_iter(--argc, ++argv);
	cmdline.description( "This program does a two table join and outputs the "
							"resulting Ks. Both table need to have gene_id1, gene_id2 "
							"fields. The Ks table need to have a Ks field.");
	if (cmdline.parse(argv_iter))
		return false;

	// make sure the error log file is valid
	string err_log			= opt_err_log.str();
	args.log_to_file		= err_log.length();
	if (args.log_to_file)
	{
		args.f_err_log.open(err_log.c_str(), std::ios_base::out | std::ios_base::ate);
		if (!args.f_err_log.good())
		{
			cerr << "ERROR!! Could not open the error log file (" << err_log << ").\n";
			return(false);
		}
	}
	

	// define verbose first before any error messages generated
	args.verbose	= opt_verbose;


	// initialize postgresql login
	args.db_login = "dbname=" +opt_dbname.str();
	
	string user		= opt_user;
	if (opt_user.str().length())
		args.db_login += " user=" + opt_user.str();
	else
		args.db_login += " user=postgres";
	
	if (opt_host.str().length())
		args.db_login += " host=" + opt_host.str();
	
	if (opt_password.str().length())
		args.db_login += " password=" + opt_password.str();

	if (opt_login.str().length())
		args.db_login += ' ' + opt_login.str();
	
	// ks and filter values
	args.ks_cutoff       =	opt_ks_cutoff			;
	args.filter_cutoff   =	opt_filter_cutoff		;
	args.ks_table        =	opt_ks_table 			;
	args.filter_column   =	opt_filter_column		;
	args.filter_table    =	opt_filter_table		;
	args.filter_less_than=	opt_filter_less_than	;
	
	return true;
}

